package com.efast.cafe.portal.controller.common;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.portal.bean.common.PortalCommonCollectionBean;
import com.efast.cafe.portal.entity.company.PortalCompanyUser;
import com.efast.cafe.portal.service.common.IPortalCommonCollectionService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;
/**
 * 工作日曆
 * @author fxm
 *
 */
@Controller
public class PortalCommonCollectionController extends AbstractController{
	@Autowired
	private IPortalCommonCollectionService portalCommonCollectionService;
	@Autowired
	private IPortalCompanyUserService userService;
	
	
	
	/**
	 * 跳转到工作日历页面
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/toCollectionPage")
	public String toCollectionPage(HttpServletRequest request,ModelMap model,HttpSession session){
		Integer year = Calendar.getInstance().get(Calendar.YEAR);
		List<PortalCommonCollectionBean> list = portalCommonCollectionService.queryRestDayByYear(year,null);
		model.put("collectionArray", new JSONArray(list).toString());
		model.put("currentYear", year);
		String companyCode = session.getAttribute(CommonConstant.CURRENT_LOGIN_USER_COMPANYCODE_KEY).toString();
		List<PortalCompanyUser> userList = userService.queryListByCompany(companyCode);
		model.put("userList",new JSONArray(userList).toString());
		return "/common/collection";
	}
	
	/**
	 * 初始化企业工作日历 默认周末为休息日
	 * @param year
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/initCompanyWorkingDay")
	public @ResponseBody ResultBean initCompanyRestDays(@RequestParam Integer year){
		try{
			portalCommonCollectionService.initCompanyRest(year);
			return new ResultBean(true,ADD_SUCCESS_TIP);
		}catch(Exception e){
			return new ResultBean(false, ADD_FAILURE_TIP);
		}
	}
	
	/**
	 * 休息日列表
	 * @param year yyyy
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/queryRestDayByYear")
	public @ResponseBody List<PortalCommonCollectionBean> queryRestDayByYear(
			@RequestParam Integer year,@RequestParam(required=false) String username){
		List<PortalCommonCollectionBean> list = portalCommonCollectionService.queryRestDayByYear(year,username);
		return list;
	}
	
	
	
	/**
	 * 添加休息日
	 * @param msg
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/addRestDay")
	public @ResponseBody ResultBean addRestDay(@RequestParam Integer year,
			@RequestParam String startDate,@RequestParam String endDate,@RequestParam(required=false) String username){
		try{
			portalCommonCollectionService.addRestDay(year, startDate, endDate, username);
			return new ResultBean(true,ADD_SUCCESS_TIP);
		}catch(Exception e){
			return new ResultBean(false, ADD_FAILURE_TIP);
		}
	}
	
	/**
	 * 初始化用户的工作日
	 * @param msg
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/initUserRestDay")
	public @ResponseBody ResultBean delRestDay(@RequestParam Integer year,@RequestParam String username){
		try{
			portalCommonCollectionService.delRestDay(year, username, null, null);
			return new ResultBean(true,DEL_SUCCESS_TIP);
		}catch(Exception e){
			return new ResultBean(false, DEL_FAILURE_TIP);
		}
	}
	
	/**
	 * 刪除用户或企业默认的休息日
	 * @param msg
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/delRestDay")
	public @ResponseBody ResultBean delUserOrCompanyRestDay(@RequestParam Integer year,@RequestParam String username,
			@RequestParam String startDate,@RequestParam String endDate){
		try{
			portalCommonCollectionService.delRestDay(year, username, startDate, endDate);
			return new ResultBean(true,DEL_SUCCESS_TIP);
		}catch(Exception e){
			return new ResultBean(false, DEL_FAILURE_TIP);
		}
	}
}
